from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import json

def keep_data():
    options = Options()
    # 设置浏览器无痕窗口
    options.add_argument('--incognito')
    # # 去掉正在被自动化测试工具测试
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-automation'])

    driver = webdriver.Chrome(executable_path=r"D:\六星教育\09期\2021_09_python\day15-selenium使用\代码\chromedriver.exe", options=options)

    with open('cookie_w.txt', 'r', encoding='utf8') as f:
        listCookies = json.loads(f.read())
    driver.get('https://www.bilibili.com/')
    time.sleep(2)
    for cookie in listCookies:
        cookie_dict = {
            'domain': '.bilibili.com',
            'name': cookie.get('name'),
            'value': cookie.get('value'),
            "expires": '',
            'path': '/',
            'httpOnly': False,
            'HostOnly': False,
            'Secure': False
        }
        driver.add_cookie(cookie_dict)
    driver.refresh()
    time.sleep(4)
    driver.close()
    driver.quit()
if __name__ == '__main__':
    keep_data()


